package models

import (
	"time"
)

// Cardholder 持卡人模型
type Cardholder struct {
	ID           int64     `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	FirstName    string    `gorm:"column:first_name;type:varchar(50);not null" json:"first_name"`
	LastName     string    `gorm:"column:last_name;type:varchar(50);not null" json:"last_name"`
	Email        string    `gorm:"column:email;type:varchar(100);uniqueIndex:uk_email;not null" json:"email"`
	Mobile       string    `gorm:"column:mobile;type:varchar(20);uniqueIndex:uk_mobile;not null" json:"mobile"`
	MobilePrefix string    `gorm:"column:mobile_prefix;type:varchar(10);default:'+1'" json:"mobile_prefix"`
	Nationality  string    `gorm:"column:nationality;type:varchar(50);default:'US'" json:"nationality"`
	Status       string    `gorm:"column:status;type:varchar(20);default:'ACTIVE';index:idx_status" json:"status"`
	UserID       int64     `gorm:"column:user_id;index:idx_user_id" json:"user_id"`                               // 关联用户ID
	PlatformID   string    `gorm:"column:platform_id;type:varchar(50);index:idx_platform_id" json:"platform_id"`  // 平台ID
	ExternalID   string    `gorm:"column:external_id;type:varchar(100);index:idx_external_id" json:"external_id"` // 外部平台ID
	CreatedAt    time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt    time.Time `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" json:"updated_at"`
	IsDeleted    int8      `gorm:"column:is_deleted;not null;default:0;index:idx_is_deleted" json:"is_deleted"`
}

// TableName 获取表名
func (Cardholder) TableName() string {
	return "cardholders"
}

// CardholderAddress 持卡人地址模型
type CardholderAddress struct {
	ID           int64     `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	CardholderID int64     `gorm:"column:cardholder_id;not null;index:idx_cardholder_id" json:"cardholder_id"`
	AddressType  string    `gorm:"column:address_type;type:varchar(20);default:'BILLING';index:idx_address_type" json:"address_type"` // BILLING, SHIPPING
	AddressLine1 string    `gorm:"column:address_line1;type:varchar(255);not null" json:"address_line1"`
	AddressLine2 string    `gorm:"column:address_line2;type:varchar(255)" json:"address_line2"`
	City         string    `gorm:"column:city;type:varchar(100);not null" json:"city"`
	State        string    `gorm:"column:state;type:varchar(50);not null" json:"state"`
	PostalCode   string    `gorm:"column:postal_code;type:varchar(20);not null" json:"postal_code"`
	Country      string    `gorm:"column:country;type:varchar(50);default:'US'" json:"country"`
	IsDefault    int8      `gorm:"column:is_default;type:tinyint;default:0;index:idx_is_default" json:"is_default"`
	CreatedAt    time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt    time.Time `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" json:"updated_at"`
}

// TableName 获取表名
func (CardholderAddress) TableName() string {
	return "cardholder_addresses"
}

// CardholderDocument 持卡人证件模型
type CardholderDocument struct {
	ID             int64      `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	CardholderID   int64      `gorm:"column:cardholder_id;not null;index:idx_cardholder_id" json:"cardholder_id"`
	DocumentType   string     `gorm:"column:document_type;type:varchar(50);not null;index:idx_document_type" json:"document_type"` // PASSPORT, DRIVER_LICENSE, ID_CARD
	DocumentNumber string     `gorm:"column:document_number;type:varchar(100);not null" json:"document_number"`
	IssuingCountry string     `gorm:"column:issuing_country;type:varchar(50);not null" json:"issuing_country"`
	ExpiryDate     *time.Time `gorm:"column:expiry_date" json:"expiry_date"`
	DocumentImage  string     `gorm:"column:document_image;type:text" json:"document_image"`                           // 证件图片URL或base64
	Status         string     `gorm:"column:status;type:varchar(20);default:'PENDING';index:idx_status" json:"status"` // PENDING, APPROVED, REJECTED
	CreatedAt      time.Time  `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt      time.Time  `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" json:"updated_at"`
}

// TableName 获取表名
func (CardholderDocument) TableName() string {
	return "cardholder_documents"
}

// CardholderKYC 持卡人KYC信息模型
type CardholderKYC struct {
	ID            int64      `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	CardholderID  int64      `gorm:"column:cardholder_id;not null;uniqueIndex:uk_cardholder_id" json:"cardholder_id"`
	KYCLevel      string     `gorm:"column:kyc_level;type:varchar(20);default:'BASIC';index:idx_kyc_level" json:"kyc_level"`      // BASIC, ENHANCED, FULL
	KYCStatus     string     `gorm:"column:kyc_status;type:varchar(20);default:'PENDING';index:idx_kyc_status" json:"kyc_status"` // PENDING, APPROVED, REJECTED
	DateOfBirth   *time.Time `gorm:"column:date_of_birth" json:"date_of_birth"`
	SSN           string     `gorm:"column:ssn;type:varchar(20)" json:"ssn"` // 社会安全号（加密存储）
	AnnualIncome  string     `gorm:"column:annual_income;type:varchar(50)" json:"annual_income"`
	Occupation    string     `gorm:"column:occupation;type:varchar(100)" json:"occupation"`
	Employer      string     `gorm:"column:employer;type:varchar(100)" json:"employer"`
	SourceOfFunds string     `gorm:"column:source_of_funds;type:varchar(100)" json:"source_of_funds"`
	RiskLevel     string     `gorm:"column:risk_level;type:varchar(20);default:'LOW';index:idx_risk_level" json:"risk_level"` // LOW, MEDIUM, HIGH
	ReviewNotes   string     `gorm:"column:review_notes;type:text" json:"review_notes"`
	ReviewedBy    int64      `gorm:"column:reviewed_by;index:idx_reviewed_by" json:"reviewed_by"`
	ReviewedAt    *time.Time `gorm:"column:reviewed_at" json:"reviewed_at"`
	CreatedAt     time.Time  `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt     time.Time  `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" json:"updated_at"`
}

// TableName 获取表名
func (CardholderKYC) TableName() string {
	return "cardholder_kyc"
}

// 请求和响应结构体

// CreateCardholderRequest 创建持卡人请求
type CreateCardholderRequest struct {
	FirstName    string `json:"first_name" binding:"required,max=50"`
	LastName     string `json:"last_name" binding:"required,max=50"`
	Email        string `json:"email" binding:"required,email"`
	Mobile       string `json:"mobile" binding:"required"`
	MobilePrefix string `json:"mobile_prefix"`
	Nationality  string `json:"nationality"`
	UserID       int64  `json:"user_id"`
	PlatformID   string `json:"platform_id"`
	// 地址信息
	AddressLine1 string `json:"address_line1"`
	AddressLine2 string `json:"address_line2"`
	City         string `json:"city"`
	State        string `json:"state"`
	PostalCode   string `json:"postal_code"`
	Country      string `json:"country"`
}

// UpdateCardholderRequest 更新持卡人请求
type UpdateCardholderRequest struct {
	ID           int64  `json:"id" binding:"required"`
	FirstName    string `json:"first_name" binding:"max=50"`
	LastName     string `json:"last_name" binding:"max=50"`
	Email        string `json:"email" binding:"email"`
	Mobile       string `json:"mobile"`
	MobilePrefix string `json:"mobile_prefix"`
	Nationality  string `json:"nationality"`
	Status       string `json:"status"`
}

// CardholderListRequest 持卡人列表查询请求
type CardholderListRequest struct {
	PageIndex  int64  `json:"page_index" binding:"min=1"`
	PageSize   int64  `json:"page_size" binding:"min=1,max=100"`
	FirstName  string `json:"first_name"`
	LastName   string `json:"last_name"`
	Email      string `json:"email"`
	Mobile     string `json:"mobile"`
	Status     string `json:"status"`
	UserID     int64  `json:"user_id"`
	PlatformID string `json:"platform_id"`
	KYCStatus  string `json:"kyc_status"`
	RiskLevel  string `json:"risk_level"`
}

// CardholderDetailResponse 持卡人详情响应
type CardholderDetailResponse struct {
	ID           int64     `json:"id"`
	FirstName    string    `json:"first_name"`
	LastName     string    `json:"last_name"`
	Email        string    `json:"email"`
	Mobile       string    `json:"mobile"`
	MobilePrefix string    `json:"mobile_prefix"`
	Nationality  string    `json:"nationality"`
	Status       string    `json:"status"`
	UserID       int64     `json:"user_id"`
	PlatformID   string    `json:"platform_id"`
	ExternalID   string    `json:"external_id"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
	// 关联信息
	Addresses []CardholderAddressResponse  `json:"addresses"`
	Documents []CardholderDocumentResponse `json:"documents"`
	KYC       *CardholderKYCResponse       `json:"kyc"`
	// 统计信息
	CardCount    int64   `json:"card_count"`
	TotalBalance float64 `json:"total_balance"`
}

// CardholderAddressResponse 持卡人地址响应
type CardholderAddressResponse struct {
	ID           int64  `json:"id"`
	AddressType  string `json:"address_type"`
	AddressLine1 string `json:"address_line1"`
	AddressLine2 string `json:"address_line2"`
	City         string `json:"city"`
	State        string `json:"state"`
	PostalCode   string `json:"postal_code"`
	Country      string `json:"country"`
	IsDefault    bool   `json:"is_default"`
}

// CardholderDocumentResponse 持卡人证件响应
type CardholderDocumentResponse struct {
	ID             int64      `json:"id"`
	DocumentType   string     `json:"document_type"`
	DocumentNumber string     `json:"document_number"`
	IssuingCountry string     `json:"issuing_country"`
	ExpiryDate     *time.Time `json:"expiry_date"`
	Status         string     `json:"status"`
}

// CardholderKYCResponse 持卡人KYC响应
type CardholderKYCResponse struct {
	ID            int64      `json:"id"`
	KYCLevel      string     `json:"kyc_level"`
	KYCStatus     string     `json:"kyc_status"`
	DateOfBirth   *time.Time `json:"date_of_birth"`
	AnnualIncome  string     `json:"annual_income"`
	Occupation    string     `json:"occupation"`
	Employer      string     `json:"employer"`
	SourceOfFunds string     `json:"source_of_funds"`
	RiskLevel     string     `json:"risk_level"`
	ReviewNotes   string     `json:"review_notes"`
	ReviewedAt    *time.Time `json:"reviewed_at"`
}

// CardholderListResponse 持卡人列表响应
type CardholderListResponse struct {
	Total       int64                `json:"total"`
	PageIndex   int64                `json:"page_index"`
	PageSize    int64                `json:"page_size"`
	Cardholders []CardholderListItem `json:"cardholders"`
}

// CardholderListItem 持卡人列表项
type CardholderListItem struct {
	ID         int64     `json:"id"`
	FirstName  string    `json:"first_name"`
	LastName   string    `json:"last_name"`
	Email      string    `json:"email"`
	Mobile     string    `json:"mobile"`
	Status     string    `json:"status"`
	PlatformID string    `json:"platform_id"`
	CreatedAt  time.Time `json:"created_at"`
	// 统计信息
	CardCount    int64   `json:"card_count"`
	TotalBalance float64 `json:"total_balance"`
	KYCStatus    string  `json:"kyc_status"`
	RiskLevel    string  `json:"risk_level"`
}

// CardholderStatsResponse 持卡人统计响应
type CardholderStatsResponse struct {
	TotalCardholders      int64   `json:"total_cardholders"`
	ActiveCardholders     int64   `json:"active_cardholders"`
	PendingKYC            int64   `json:"pending_kyc"`
	ApprovedKYC           int64   `json:"approved_kyc"`
	RejectedKYC           int64   `json:"rejected_kyc"`
	TotalCards            int64   `json:"total_cards"`
	TotalBalance          float64 `json:"total_balance"`
	AverageCardsPerHolder float64 `json:"average_cards_per_holder"`
}
